package com.basker.pisces.core.operator.handler;

import com.basker.pisces.core.operator.CompareOperator;
import com.basker.pisces.core.utils.ConvertUtils;

public class SqlInCompareOperatorHandler extends SqlCompareOperatorHandler {

    /**
     * 将值转换为数组
     *
     * <li>支持将"a,b,c,...,n,..."这样的String参数转换为Object[]{"a","b","c",..."n",...}
     */
    @Override
    public Object updateValue(Object value) {
        return ConvertUtils.convert2Array(value);
    }

    @Override
    protected String getParameterExpression(String fieldName, CompareOperator op, Object value,
                                            boolean isParameterExp) {
        Object[] values = (Object[]) value;

        StringBuilder valsExp = new StringBuilder("(");
        for (int i = 0; i < values.length; i++) {
            String valExp = super.getParameterExpression(fieldName, op, values[i], isParameterExp);

            if (i == values.length - 1) {
                valsExp.append(valExp + ")");
            } else {
                valsExp.append(valExp + ",");
            }
        }

        return valsExp.toString();
    }
}
